package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.UnitpriceSaplingDao;
import cn.topevery.garden.entity.po.UnitpriceSapling;
import cn.topevery.garden.entity.query.UnitpriceSaplingQuery;
import cn.topevery.garden.entity.vo.UnitpriceSaplingVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2022-02-15
 */
@Repository
public class UnitpriceSaplingDaoImpl extends BaseDaoImpl<UnitpriceSapling> implements UnitpriceSaplingDao {
    @Override
    public <P extends DbCsp> List<UnitpriceSapling> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<UnitpriceSaplingVO> listPage(UnitpriceSaplingQuery para) {
        String table = "t_unitprice_sapling as price\n" +
                "        inner join t_sys_map as map on map.c_id = price.c_map_id\n" +
                "        inner join t_sys_user as usr on usr.c_id = price.c_creator_id";

        String columns = "price.c_id as id,\n" +
                "             price.c_entity_id as entityId,\n" +
                "             price.c_map_id as mapId,\n" +
                "             map.c_name as area,\n" +
                "             price.c_name as sapling_name,\n" +
                "             price.c_variety as variety,\n" +
                "             price.c_root_radius as root_radius,\n" +
                "             price.c_sapling_height as sapling_height,\n" +
                "             price.c_crown_width as crown_width,\n" +
                "             price.c_unitprice as unitprice,\n" +
                "             price.c_remark as remark,\n" +
                "             price.c_big_class as c_big_class,\n" +
                "             price.c_small_class as c_small_class,\n" +
                "             price.c_main_variable as c_main_variable,\n" +
                "             price.c_size as c_size,\n" +
                "             price.c_unit as c_unit,\n" +
                "             usr.c_user_name as creator,\n" +
                "             DATE_FORMAT(price.c_db_create_date, '%Y-%m-%d %H:%i:%s') as create_time";

        int pageIndex = para.pageIndex - 1;
        int pageSize = para.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("price.c_id desc");
        sqlQueryMate
                .where(true, "price.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(!StringUtils.isEmpty(para.getName()), "price.c_name", ScriptConditionEnum.Eq, para.getName())
                .where(!StringUtils.isEmpty(para.getMapId()) && para.getMapId() !=0, "price.c_map_id", ScriptConditionEnum.Eq, para.getMapId())
                .where(!StringUtils.isEmpty(para.getClassId()) && para.getClassId()!=0, "price.c_small_class", ScriptConditionEnum.Eq, para.getClassId())
                .where(true, "price.c_in_use", ScriptConditionEnum.Eq, 1)


        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, UnitpriceSaplingVO.class);
    }


    public DataPager<UnitpriceSaplingVO> getHisList(UnitpriceSaplingQuery para) {
        String table = "t_unitprice_sapling as price\n" +
                "        inner join t_sys_map as map on map.c_id = price.c_map_id\n" +
                "        inner join t_sys_user as usr on usr.c_id = price.c_creator_id";

        String columns = "price.c_id as id,\n" +
                "               map.c_name as area,\n" +
                "               price.c_name as sapling_name,\n" +
                "               price.c_variety as variety,\n" +
                "               price.c_root_radius as root_radius,\n" +
                "               price.c_sapling_height as sapling_height,\n" +
                "               price.c_crown_width as crown_width,\n" +
                "               price.c_unitprice as unitprice,\n" +
                "               price.c_remark as remark,\n" +
                "               price.c_big_class as c_big_class,\n" +
                "               price.c_small_class as c_small_class,\n" +
                "               price.c_main_variable as c_main_variable,\n" +
                "               price.c_size as c_size,\n" +
                "               price.c_unit as c_unit,\n" +
                "               usr.c_user_name as creator,\n" +
                "               DATE_FORMAT(price.c_create_time, '%Y-%m-%d %H:%i:%s') as create_time";

        int pageIndex = para.pageIndex - 1;
        int pageSize = para.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("price.c_create_time desc");
        sqlQueryMate
                .where(true, "price.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(!StringUtils.isEmpty(para.getName()), "price.c_name", ScriptConditionEnum.Eq, para.getName())
                .where(!StringUtils.isEmpty(para.getMapId()) && para.getMapId() !=0, "price.c_map_id", ScriptConditionEnum.Eq, para.getMapId())
                .where(!StringUtils.isEmpty(para.getClassId()) && para.getClassId()!=0, "price.c_small_class", ScriptConditionEnum.Eq, para.getClassId())
                .where(true, "price.c_in_use", ScriptConditionEnum.Eq, 1)


        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, UnitpriceSaplingVO.class);
    }

    @Override
    public UnitpriceSapling getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }
}
